// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <tests/dwio/text/writer/file_reader_util.h>

namespace kumo::pollux::text {
    std::string readFile(const std::string &name) {
        std::ifstream file(name);
        std::string line;

        std::stringstream ss;
        while (std::getline(file, line)) {
            ss << line;
        }
        return ss.str();
    }

    std::vector<std::vector<std::string> > parseTextFile(const std::string &name) {
        std::ifstream file(name);
        std::string line;
        std::vector<std::vector<std::string> > table;

        while (std::getline(file, line)) {
            std::vector<std::string> row = splitTextLine(line, TextFileTraits::kSOH);
            table.push_back(row);
        }
        return table;
    }

    std::vector<std::string> splitTextLine(const std::string &str, char delimiter) {
        std::vector<std::string> result;
        std::size_t start = 0;
        std::size_t end = str.find(delimiter);

        while (end != std::string::npos) {
            result.push_back(str.substr(start, end - start));
            start = end + 1;
            end = str.find(delimiter, start);
        }

        result.push_back(str.substr(start)); // Add the last part
        return result;
    }
} // namespace kumo::pollux::text
